package com.boxin.ims.modules.wechat.web.front;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boxin.framework.base.dao.DaoHelper;
import com.boxin.framework.base.utils.RegexUtil;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.momarketing.service.QRCodeService;
import com.boxin.ims.modules.wechat.entity.UserBusiness;
import com.boxin.ims.modules.wechat.entity.UserDefine;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;


/**
 * 产品Controller
 * @author Jakemanse
 * @version 2013-06-28
 *  中奖者公告
 */
@Controller
@RequestMapping(value = Global.FRONT_PATH+"/wechat/lottery")
public class LotteryFrontController  {
	
	
	@Autowired
	private DaoHelper daoHelper ;
	@Autowired
	private QRCodeService QRCodeService;
	
	
	@RequestMapping(value="gainers")
	public String gainer( HttpServletRequest request, HttpServletResponse response, Model model) {
		
		String uid = request.getParameter("uid");
		List<Map<String,Object>> allgainers = daoHelper.queryForList("com.boxin.ims.wechat.queryAllGainers", uid);
		
		model.addAttribute("gainerList", allgainers);
		model.addAttribute("uid",uid);
		
		return  "modules/wechat/front/lotteryGainers";
	}
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-13  上午12:12:53
	 * @function <p> 数据罗盘  </p>
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value="digitalCompass")
	public String digitalCompass( HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return  "modules/wechat/front/games/digitalCompass";
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-13  上午12:25:21
	 * @function <p> 圆软盘 </p>
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value="circleCompass")
	public String circleCompass( HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return  "modules/wechat/front/games/circleCompass";
	}
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-26  下午11:05:02
	 * @function <p> 查看二维码 </p>
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value="viewqrcode")
	public String viewQRcode( HttpServletRequest request, HttpServletResponse response, Model model) {
		try {
			String id = request.getParameter("id");
			QRCode qrCode =  QRCodeService.get(Long.parseLong(id));
			String content = qrCode.getContent();
			
			
			
			String url = QRCodeUtils.getQRCodePNGURL(Long.parseLong(id));
			model.addAttribute("url", url);
			
			String regex = "(\\d+)-";
			String uid  = RegexUtil.match(regex, content);
			uid = uid.replaceAll("-", "");
			model.addAttribute("uid", uid);
			
			UserDefine userDefine = 	WechatUtils.getUserDefineByUserId(Long.parseLong(uid));
			if(userDefine!=null && !StringUtils.isEmpty(userDefine.getQrcodeMessage())){
				model.addAttribute("qrcodeMessage", userDefine.getQrcodeMessage());
			}
		} catch (Exception e) {
			
		}
		return  "modules/wechat/front/viewQRcode";
	}
	
	
	
	
	
	
	
	
	
	
	public static void main(String[] args) {
		String content = "CCS22-8F83DFA39B274727B2649BBCA530325D";
		String regex = "\\d+-";
		Pattern pattern = Pattern.compile(regex);
		String matstr = RegexUtil.match(regex, content);
		Matcher matcher =  pattern.matcher(content);
		if(matcher.matches()){
			String m = matcher.group();
			System.out.println(m);
		}
		
	}
	
	
	
	
	
	
	

}
